package com.fsc.comadapoc1.application
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.domain.UserModel;
	import com.fsc.comadapoc1.entities.TradeRequest;
	import com.fsc.comadapoc1.entities.trade.Fund;
	import com.fsc.comadapoc1.entities.trade.Portfolio;
	import com.fsc.comadapoc1.entities.trade.Trade;
	import com.fsc.comadapoc1.events.CreateTradesEvent;
	import com.fsc.comadapoc1.events.LoadPortfolioEvent;
	import com.fsc.comadapoc1.events.PanelChangeEvent;
	
	import flash.events.EventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.remoting.RemoteObject;

	[Event(name="subscribeTradeComplete",type="com.fsc.comadapoc1.events.CreateTradesEvent")]
	[Event(name="changeState",type="com.fsc.comadapoc1.events.PanelChangeEvent")]
	[Event(name="loadPortfolio",type="com.fsc.comadapoc1.events.LoadPortfolioEvent")]
	[ManagedEvents(names="subscribeTradeComplete,changeState,loadPortfolio")]
	public class CreateTradesCommand extends EventDispatcher
	{
		public function CreateTradesCommand()
		{
		}
		
		
		[Inject(id="portfolioService")]
		public var service:RemoteObject;
		
		/**
		 * Contains objects to manage portfolios
		 */ 
		[Inject]
		[Bindable]
		public var model:PortfolioModel;
		
		[Inject]
		[Bindable]
		public var userModel:UserModel;
		
		private var _portfolioID : String;
		private var inited:Boolean = false;
		
		
		
		public function init():void{
			trace("Initializing Search Command");
			inited = true;
		}
		
		
		public function execute(event:CreateTradesEvent):AsyncToken {
			if(!inited){
				init();
			}
			
			var tr:TradeRequest = event.tradeRequest;
			//CreateSubscription(sessionToken:GUID, portfolioId:int, fundId:int, application:Date, deal:Date, amount:Number, notes:String);
			
			return service.CreatesSubscription( userModel.guid,
												model.currentPortfolio.id,
												model.currentPortfolio.selectedFund.id,
												tr.initiated,
												tr.valueDealingDate,
												tr.amount,
												tr.notes
			) as AsyncToken;
		}
		
		/******************************************
		 *  Result handler 
		 */
		public function result(data:Object):void
		{
			var subscribeComplete:CreateTradesEvent = new CreateTradesEvent(CreateTradesEvent.SUBSCRIBE_TRADE_COMPLETE);
			
			if ( data.Results != null)
			{
				dispatchEvent(subscribeComplete);
				Alert.show("Subscribe Request Complete")
				dispatchEvent(new LoadPortfolioEvent(LoadPortfolioEvent.LOAD_PORTFOLIO,model.currentPortfolio));
				dispatchEvent(new PanelChangeEvent(PanelChangeEvent.PORTFOLIO_VIEW_STATE));
			}
		}
		
		/******************************************
		 *  Fault handler in case the request failed. 
		 */
		public function error (fault:FaultEvent, trigger:CreateTradesEvent) : void {
			//need to add some logging here
			Alert.show(fault.fault.message,fault.fault.faultDetail);
		}  
	}
}



	
